﻿using famails.common;
using famails.dto;
using famails.bus;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.template
{
    public partial class BackEnd : System.Web.UI.MasterPage
    {
        UserLoginDTO userLogin;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            checkRole();
            if (!IsPostBack)
            {
                LoadMainMenu();
                
            }
        }
        private void checkRole()
        {
            RoleListBUS role = new RoleListBUS();
            DataTable menu = role.GetSubMenuByDepartmentId(userLogin.UserType);
            bool result=false;
            foreach (DataRow row in menu.Rows)
            {
                string link = "/"+row["rolelink"] + "";
                result = Request.Url.PathAndQuery.ToLower().IndexOf(link.ToLower()) >= 0;
                if (link!="/"&&result)
                {
                    lblGoupName.Text = role.GetByRoleId(Convert.ToInt32(row["parentid"]+"")).Rows[0]["roleName"].ToString().Replace(" ( Chức năng chính )", "");
                    Session["groupId"] = row["parentId"];
                    Session["linkId"] = row["roleId"];
                    break;
                }
            }
            if (result || userLogin.Username.ToLower() == "administrator") return;
            if (!result && userLogin.Username.ToLower() != "administrator"
                && (Request.Url.PathAndQuery.ToLower().IndexOf("clientdetail.aspx") >= 0
                || Request.Url.PathAndQuery.ToLower().IndexOf("change-pass.aspx") >= 0
                || Request.Url.PathAndQuery.ToLower().IndexOf("forgotpassword.aspx") >= 0
                || Request.Url.PathAndQuery.ToLower().IndexOf("packagechange.aspx") >= 0)
                ) return;
            else Response.Redirect("~/backend/page/login.aspx?rUrl=" + Request.Url.PathAndQuery);
        }
        private void CheckUrl()
        {
            string role = "";
            UserLoginDTO ulDto = getUserLogin();
            string url = Path.GetFileName(Request.PhysicalPath);
            DataTable dt = Common.GetRoleIdByName(url);
            if (dt.Rows.Count > 0)
            {
                role = dt.Rows[0]["roleId"].ToString();
                int UserType = ulDto.UserType;
                bool isAllowed = Common.checkRoleByRoleId(int.Parse(role), UserType);
                if (isAllowed == false)
                {
                    Response.Redirect("clientregister.aspx");
                }
            }
        }
        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {                
                userLogin = (UserLoginDTO)Session["us-login"];
                if (userLogin.UserType != (int)common.Common.DepartmentId.Admin)
                {
                    DataTable client = new ClientBUS().GetByClientID(userLogin.ClientID);
                    if (Convert.ToDateTime(client.Rows[0]["expiredate"]) < DateTime.Now.Date)
                    {
                        Response.Redirect("ClientDetail.aspx");
                    }
                }
                return userLogin;
            }
            else Response.Redirect("~/backend/page/login.aspx?rUrl=" + Request.Url.PathAndQuery);
            return null;
        }

        protected void lbtMenu_Click(object sender, EventArgs e)
        {
            LinkButton link = (LinkButton)sender;
            Session["linkId"] = link.Attributes["roleId"];
            Response.Redirect(link.CommandArgument);

        }
        void LoadMainMenu()
        {
            bus.RoleListBUS user = new bus.RoleListBUS();
            DataTable menu = user.GetSubMenuByParentId(userLogin.UserType, Session["groupId"]);
            rptMenu.DataSource = menu;
            rptMenu.DataBind();
        }
    }
}